package com.szc.demo01.controller;

import com.szc.demo01.controller.base.BaseController;
import com.szc.demo01.exception.NotBossException;
import com.szc.demo01.pojo.Notice;
import com.szc.demo01.service.NoticeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;

/*
公告管理控制器
 */
@Controller
public class NoticeController extends BaseController {

    @Resource
    private NoticeService noticeService;

    /**
     * 查询所有公告
     */
    @RequestMapping("/selectnotice")
    public String selectnotice(Model model){
        model.addAttribute("n",noticeService.noticelist()) ;
        return "notice/noticeShow";
    }


    /**
     *跳转到新增公告页面
     */
    @RequestMapping("/to_notice")
    public String enterrelease(Model model) throws NotBossException {
        //这里需要判断用户有无新增公告权限
        assertUserIdHasPerm(3);
        return "notice/noticeAdd";
    }


    /**
     *新增公告到数据库NoticeController
     */
    @RequestMapping("/boss_add_notice")
    public String add(Notice notice,String nmark) throws NotBossException {
        assertUserIdHasPerm(3);
        notice.setEid(getUserId());
        notice.setNmark(nmark);
        notice.setEdate(new Date());
        this.noticeService.save(notice);
        return  "redirect:/selectnotice";
    }

    /**
     *根据公告id删除
     */
    @RequestMapping("/boss_delete_notice")
    public String delete(Integer nid) throws Exception{
        this.noticeService.removeById(nid);
        return "redirect:/selectnotice";
    }

}
